package PowerWord.Core.Plan;

import DataOperator.Loader;
import PowerWord.Core.Data.Defines;
import PowerWord.Core.Data.FullInfo;
import PowerWord.Core.Data.Sound;
import PowerWord.Core.Data.WebDict;
import PowerWord.Core.Data.WordList;
import PowerWord.Core.ReciteEffect.WordProperty.FamiliarLevel;
import SimpleDict.DictData;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Controller {
	static final String SavFileName = "Save";
	
	public static final int NothingFlag = 0x0;
	public static final int ChangeIndexFlag = 0x01;
	public static final int PlaySoundFlag = 0x04;
	public static final int IndexSoundFlag = ChangeIndexFlag | PlaySoundFlag;
	
	public static final int WordInterval = 3;
	public static boolean WebMode = false;
	public static ConnectivityManager NetManager = null;
	
	private Loader mLoader = new Loader();
	private WebDict mWebDict = new WebDict();
	private Sound mSound = new Sound();
	private WordList mWordList = new WordList();
	
	private Duty mDuty = null;

	private static Controller instance = null;

	public static Controller Instance() {
		if(instance == null) {
			instance = new Controller();
			instance.Init();
		}
		return instance;
	}

	private Controller() {
	}
	
	private void Init() {
		mLoader.Init(Defines.IndexPath, Defines.DictPath);
        mSound.Init();
        mWordList.Import("WordList.txt");
	}
	
	public void Release() {
		mLoader.Release();
        mSound.Release();
        mWordList.Export();
        instance = null;
	}
	
	// study
	public void ChangeWordList(String listFileName) {
		mWordList.Import(listFileName);
	}
	
	public void SwitchStudyMode(boolean isPlanMode){
		mDuty = isPlanMode ? new Group(mWordList) : new Free(mWordList);
	}
	
	public void InitDuty(String arg0, String arg1, int flag){
		mDuty.CreateWork(arg0, arg1, flag);
	}
	// system
	public int GetDutySize(){
		if(mDuty != null){
			return mDuty.subList.length;
		}
		return 0;
	}
	
	public Duty GetDuty(){
		return mDuty;
	}
	
	public int GetId(int pos){
		return mDuty.subList[pos];
	}
	
	public String GetItem(int pos){
		return mWordList.GetItem(GetId(pos));
	}

	public DictData GetData(int pos){
		return mLoader.LoadSimpleWordInfo(GetItem(pos));
	}
	
	public FullInfo GetFullInfo(int pos){
		assert(pos < mDuty.GetCount());
		FullInfo rev = new FullInfo();
		rev.word = GetItem(pos);
		rev.data = GetData(pos);
		return rev;
	}
	
	public void SetWordFamiliar(int pos, FamiliarLevel e){
		mWordList.SetItemProperty(GetId(pos), e);
	}
	
	public int GetWordFamiliar(int pos){
		return mWordList.GetItemFamiliar(GetId(pos));
	}
	
	public void SetWordProperty(int pos, byte flag){
		mWordList.SetItemProperty(GetId(pos), flag);
	}
	
	public int GetCount(){
		return mDuty.subList.length;
	}
	
	public void PlaySound(int off, int len) {
		mSound.Play(off, len);
	}
	
	public void Random(){
		mDuty.Random();
	}
	
	public void Order(){
		mDuty.Order();
	}
	
	public String WebSearch(String word) {
		return mWebDict.Search(word);
	}
	
	public void ToggleWebSurvice(boolean mode, ConnectivityManager manager) {
		WebMode = mode;
		NetManager = manager;
	}
	
	public static boolean IsWebOn() {
		if(WebMode && NetManager != null) {
			NetworkInfo info = NetManager.getActiveNetworkInfo();
			return info != null && info.isConnected();
		}
		return false;
	}
}
